package com.junxian.springboot03.common;

import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

/**
 * String 测试类
 */
public class TestString {

    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("192.168.31.90", 6379);
        jedis.auth("123456");

        //清空数据库
        jedis.flushDB();
        System.out.println("=========增加数据=========");
        System.out.println(jedis.set("k1", "v1"));
        System.out.println(jedis.set("k2", "v2"));
        System.out.println(jedis.set("k3", "v3"));
        System.out.println(jedis.set("k4", "v4"));
        System.out.println("删除k2："+jedis.del("k2"));
        System.out.println("获取k2："+jedis.get("k2"));//get可以获取单个键值
        System.out.println("修改k1的值："+jedis.set("k1", "new v1"));
        System.out.println("在k3后面加入值："+jedis.append("k3", "-append"));
        System.out.println("获取k3的值："+jedis.get("k3"));
        System.out.println("增加多个键值对："+jedis.mset("k11","v11","k22","v22","k33","v33"));
        System.out.println("获取多个键值对："+jedis.mget("k11", "k22", "k33"));//mget可以获取多个键值
        System.out.println("删除多个键值对："+jedis.del("k11", "k22"));
        System.out.println("获取多个键值对："+jedis.mget("k11", "k22", "k33"));

        jedis.flushDB();
        System.out.println("=========新增键值对防止覆盖原先的值=========");
        System.out.println(jedis.setnx("k1", "v1"));
        System.out.println(jedis.setnx("k2", "v2"));
        System.out.println(jedis.setnx("k2", "new-v2"));
        System.out.println("获取k1 k2的值："+jedis.mget("k1", "k2"));

        System.out.println("=========设置过期时间=========");
        System.out.println(jedis.setex("k3", 2, "v3"));
        System.out.println("获取k3的值："+jedis.mget("k3"));

        TimeUnit.SECONDS.sleep(3);

        System.out.println("再次获取k3的值："+jedis.mget("k3"));

        System.out.println("=========获取原值更新为新的值=========");
        System.out.println("获取原值更新为新的值:" +jedis.getSet("k2", "new_value2"));
        System.out.println("获取k2的值："+jedis.mget("k2"));

        System.out.println("截取字符串的k2的值："+jedis.getrange("k2", 1, 5));
    }
}
